package com.ruoyi.system.controller.H5;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.domain.BaseContest;
import com.ruoyi.system.domain.BaseTournament;
import com.ruoyi.system.domain.dto.H5.ContestGoodNumDto;
import com.ruoyi.system.domain.dto.H5.PersonalDataDto;
import com.ruoyi.system.service.IBaseContestService;
import com.ruoyi.system.service.IBaseTournamentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * h5赛事Controller
 * 
 * @author wangbj
 * @date 2024-04-16
 */
@ApiOperation("赛事")
@RestController
@RequestMapping("/h5/tournament")
public class BaseTournamentH5Controller extends BaseController
{
    @Autowired
    private IBaseTournamentService baseTournamentService;
    @Autowired
    private IBaseContestService baseContestService;
    @Autowired
    private RedisService redisService;

    /**
     * 查询比赛数量
     */
    @GetMapping("/getTournamentNum")
    @Operation(summary = "查询比赛数量", description = "查询比赛数量")
    public AjaxResult getTournamentNum(Long contestId) {
        LambdaQueryWrapper<BaseTournament> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseTournament::getContestId,contestId);
        wrapper.eq(BaseTournament::getIsDeleted,0);
        return AjaxResult.success(baseTournamentService.count(wrapper));
    }

    /**
     * 获取点赞量
     */
    @GetMapping("/getGoodNum")
    @Operation(summary = "获取点赞量", description = "获取点赞量")
    public AjaxResult getGoodNum(Long contestId){
        Integer cacheObject = Convert.toInt(redisService.getCacheObject(Constants.GOOD_NUM + contestId));
        if (Objects.isNull(cacheObject) || cacheObject == 0){
            BaseContest contest = baseContestService.getById(contestId);
            if (Objects.nonNull(contest)){
                return AjaxResult.success(contest.getGoodNum());
            }
        }
        return AjaxResult.success(cacheObject);
    }

    /**
     * 点赞
     */
    @PostMapping("/saveGood")
    @Operation(summary = "点赞", description = "点赞")
    public AjaxResult saveGood(@RequestBody @Validated ContestGoodNumDto contestGoodNumDto){
        Object cacheObject = redisService.getCacheObject(Constants.GOOD_NUM + contestGoodNumDto.getContestId());
        long num = cacheObject == null ? 0 : Long.valueOf((Integer) cacheObject);
        num++;
        redisService.setCacheObject(Constants.GOOD_NUM + contestGoodNumDto.getContestId(),num);
        return AjaxResult.success(num);
    }

    /**
     * 个人数据生成
     */
    @PostMapping("/personalData")
    @Operation(summary = "个人数据生成", description = "个人数据生成")
    private AjaxResult personalData(@RequestBody @Validated PersonalDataDto dto){
        return AjaxResult.success(baseTournamentService.personalData(dto));
    }
}
